package com.zhongen.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongen.admin.api.entity.SysDept;
import com.zhongen.admin.api.entity.SysFile;
import com.zhongen.admin.api.feign.RemoteDeptService;
import com.zhongen.common.core.constant.SecurityConstants;
import com.zhongen.common.security.service.PigUser;
import com.zhongen.common.security.util.SecurityUtils;
import com.zhongen.oa.entity.ContractFileEntity;
import com.zhongen.oa.entity.ProjectContractEntity;
import com.zhongen.oa.entity.ProjectFileEntity;
import com.zhongen.oa.mapper.ProjectContractMapper;
import com.zhongen.oa.service.ContractFileService;
import com.zhongen.oa.service.ProjectContractService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 项目合同
 *
 * @author Code Generator
 * @date 2024-01-18 14:42:21
 */
@Service
@AllArgsConstructor
public class ProjectContractServiceImpl extends ServiceImpl<ProjectContractMapper, ProjectContractEntity>
		implements ProjectContractService {

	private final ContractFileService contractFileService;

	private final RemoteDeptService remoteDeptService;

	@Override
	public IPage<ProjectContractEntity> queryContractInfoList(Page page, ProjectContractEntity projectContract) {
		PigUser user = SecurityUtils.getUser();
		List<SysDept> deptList = remoteDeptService.listDescendant(user.getDeptId(), SecurityConstants.FROM_IN);
		HashSet<Long> deptIds = new HashSet<>();
		deptIds.add(user.getDeptId());
		deptList.forEach(sysDept -> {
			deptIds.add(sysDept.getDeptId());
		});

		return baseMapper.queryContractInfoList(page, projectContract, deptIds);
	}

	@Override
	public ProjectContractEntity getContractInfoById(Long id) {
		return baseMapper.getContractInfoById(id);
	}

	@Override
	public int addProjectContract(ProjectContractEntity projectContract) {
		PigUser user = SecurityUtils.getUser();
		projectContract.setDeptId(user.getDeptId());
		// 根据项目编号查询合同序号
		ProjectContractEntity result = baseMapper.selectOne(Wrappers.<ProjectContractEntity>query()
			.eq("project_number", projectContract.getProjectNumber())
			.select("IFNULL(MAX(contract_sort),0) as contract_sort"));
		// 生成合同编号,维护序号
		projectContract.setContractSort(result.getContractSort() + 1);
		projectContract.setContractNumber(projectContract.getProjectNumber() + "-"
				+ projectContract.getSignDate().format(DateTimeFormatter.ofPattern("yyMM"))
				+ formatNumberToThreeDigits(result.getContractSort() + 1));
		int insert = baseMapper.insert(projectContract);
		// 插入附件
		ArrayList<ContractFileEntity> contractFileEntities = new ArrayList<>();
		List<SysFile> fileList = projectContract.getFileList();
		if (null != fileList && fileList.size() > 0) {
			for (SysFile file : fileList) {
				ContractFileEntity contractFileEntity = new ContractFileEntity();
				contractFileEntity.setContractId(projectContract.getId());
				contractFileEntity.setFileId(Long.valueOf(file.getId()));
				contractFileEntities.add(contractFileEntity);
			}
			contractFileService.saveBatch(contractFileEntities);
		}
		return insert;
	}

	@Override
	public boolean updateById(ProjectContractEntity projectContract) {
		baseMapper.updateById(projectContract);
		// 插入附件
		ArrayList<ContractFileEntity> contractFileEntities = new ArrayList<>();
		List<SysFile> fileList = projectContract.getFileList();
		if (null != fileList && fileList.size() > 0) {
			contractFileService
				.remove(new QueryWrapper<ContractFileEntity>().eq("contract_id", projectContract.getId()));
			for (SysFile file : fileList) {
				ContractFileEntity contractFileEntity = new ContractFileEntity();
				contractFileEntity.setContractId(projectContract.getId());
				contractFileEntity.setFileId(Long.valueOf(file.getId()));
				contractFileEntities.add(contractFileEntity);
			}
			contractFileService.saveBatch(contractFileEntities);
		}
		return true;
	}

	private String formatNumberToThreeDigits(long number) {
		String numberString = String.valueOf(number);
		if (numberString.length() < 3) {
			numberString = String.format("%02d", number);
		}
		return numberString;
	}

}
